package com.example.reagent_management.ui.main;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.example.lib_base.base.BaseDBFragment;
import com.example.lib_base.base.DataBindingConfig;
import com.example.lib_base.event.ShareViewModel;
import com.example.reagent_management.BR;
import com.example.reagent_management.R;
import com.example.reagent_management.ui.history.HistoryFragment;
import com.example.reagent_management.ui.home.HomeFragment;
import com.example.reagent_management.ui.setting.SettingFragment;
import com.example.reagent_management.ui.setting.TestFragment;
import com.example.reagent_management.ui.statistics.StatisticsFragment;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MainFragment extends BaseDBFragment {
    private static final String TAG = "MainFragment";

    private MainFragmentViewModel mState;
    private FragmentStateAdapter fragmentStateAdapter;
    private ShareViewModel mEvent;


    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(MainFragmentViewModel.class);
        mEvent = getApplicationScopeViewModel(ShareViewModel.class);
    }

    @Override
    protected void initViewOrObserve() {
        //不推荐  binding获取到的view实例可能为空  kotlin中可简化view的null判断 java要写if-null
//        final FragmentMainBinding binding = (FragmentMainBinding) getBinding();

        mState.getPage().observe(this,integer -> {
            mState.currentPage.setValue(integer);
        });
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        fragmentStateAdapter = new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                if (position == 0) {
                    return new HomeFragment();
                } else if (position == 1) {
                    return new HistoryFragment();
                } else if (position == 2) {
                    return new StatisticsFragment();
                } else if (position == 3) {
//                    return new SettingFragment();
                    return new TestFragment();
                } else {
                    return new HomeFragment();
                }
            }

            @Override
            public int getItemCount() {
                return 4;
            }
        };
        return new DataBindingConfig(R.layout.fragment_main, BR.vm, mState)
                .addBindingParam(BR.event,mEvent)
                .addBindingParam(BR.adapter, fragmentStateAdapter)
                .addBindingParam(BR.controller, new EventProxy());
    }

    public  class EventProxy {
        public NavigationBarView.OnItemSelectedListener listener() {
           return new BottomNavigationView.OnItemSelectedListener() {
                @SuppressLint("NonConstantResourceId")
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.menu_main:
                            mState.setPage(0);
                            return true;
                        case R.id.menu_project:
                            mState.setPage(1);
                            return true;
                        case R.id.menu_system:
                            mState.setPage(2);
                            return true;
                        case R.id.menu_public:
                            mState.setPage(3);
                            return true;
                    }
                    return true;
                }
            };
        }
    }
}
